class Category < ActiveRecord::Base
  has_many :books  
  has_many :category_items
  before_destroy :ensure_not_referenced_by_any_category_item, :ensure_not_referenced_by_any_book
  validates :name, :presence => true, :uniqueness => true
  
  def ensure_not_referenced_by_any_category_item
    if category_items.empty?
      return true
    else
      errors.add(:base, 'Category Items present')
      return false
    end
  end
  
  def ensure_not_referenced_by_any_book
    if books.empty?
      return true
    else
      errors.add(:base, 'Books present')
      return false
    end
  end
end
